<?php

namespace App\Http\Controllers\home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

//个人中心
class Personal extends Common
{
    //
    public function index()
    {
        $data['member'] = Auth::guard('member')->user();
        $data['menus'] = DB::table('member_menu')->where('pid',0)->where('status',0)->lists();

        return view('home.per-index',$data);
    }
    public function ge_index(Request $request)
    {
        $data['member'] = Auth::guard('member')->user();
        $data['id']  = $data['member']->id;

        $data['item'] = DB::table('member')->where('id',$data['id'])->item();

        return view('home.per-ge',$data);
    }
    public function ge_save(Request $request)
    {
        $data['id']  = $request->id;
        $data['user_name']  = $request->user_name;
        $data['nick_name']  = $request->nick_name;
        $data['email']  = $request->email;

        $username = DB::table('member')->where('user_name',$data['user_name'])->first();
        if($username){
            exit(json_encode(array('code'=>1,'msg'=>'该用户名已存在')));
        }
        $email = DB::table('member')->where('email',$data['email'])->first();
        if($email){
            exit(json_encode(array('code'=>1,'msg'=>'该邮箱已存在')));
        }
        DB::table('member')->where('id',$data['id'])->update($data);



        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }
    public function article_index(Request $request)
    {
        $data['member'] = Auth::guard('member')->user();
        $data['mid']  = $data['member']->id;

        $data['cate']  = DB::table('article_cate')->cates('id');
        $data['admin']   = DB::table('member')->cates('id');
        //实现分页
        $data['page'] = (int)$request->page;//获取分页
        $data['pageSize'] = 10;
        $pages = DB::table('article')->where('mid', $data['mid'])->orderBy('id','desc')->pages($data['pageSize']);
        $data['article_list'] = $pages['lists'];
        $data['total']        = $pages['total']; //总数
        return view('home.article_index',$data);
    }
    //用户文章添加
    public function article_add(Request $request)
    {
        $aid = (int)$request->aid;
        $data['article'] = DB::table('article')->where('id',$aid)->item();
        $data['article_contents'] = DB::table('article_detail')->where('aid',$aid)->item();
        $data['cates']    = DB::table('article_cate')->lists();
        $data['labels']    = DB::table('article_label')->lists();
        return view('home.article_add',$data);
    }
    public function article_save(Request $request)
    {

        $aid              = (int)$request->aid;
        $data['cid']      = (int)$request->cid;
        $data['title']    = trim($request->title);
        $data['subtitle'] = trim($request->subtitle);
        $data['thumb']    = trim($request->thumb);
        $data['keywords'] = trim($request->keywords);
        $data['descs']    = trim($request->descs);
        $data['status']   = 2;
        $member = Auth::guard('member')->user();
        $data['mid']  = $member->id;

        $contents = trim($request->contents);
        //敏感词判断
        $swd_list = DB::table('article_swd')->lists();
        foreach ($swd_list as $swd){
            if (strpos($contents,$swd['name']) != false){
                exit(json_encode(array('code'=>1,'msg'=>'文章内容含有敏感词'.$swd['name'])));
            }
        }
        //        判断
        if ($data['cid'] == ""){
            exit(json_encode(array('code'=>1,'msg'=>'请选择分类')));
        }
        if ($data['title'] == ""){
            exit(json_encode(array('code'=>1,'msg'=>'请输入标题')));
        }
        if ($aid == 0){
            $data['add_time'] = time();
            $aid = DB::table('article')->insertGetId($data);
            if ($aid>0){
                DB::table('article_detail')->insert(array('aid'=>$aid,'contents'=>$contents));
            }
        }else{
            $data['status'] = 2;
            DB::table('article')->where('id',$aid)->update($data);
            DB::table('article_detail')->where('aid',$aid)->update(array('aid'=>$aid,'contents'=>$contents));
        }
        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }
}
